package com.kgc.warehouse.system.controller;

import cn.hutool.captcha.LineCaptcha;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/captcha")
public class CaptchaController {

    @Resource
    private RedisTemplate<String,String> redisTemplate;
    @RequestMapping("/getCaptcha")
    public void getCaptcha(HttpServletResponse response, HttpSession session) throws IOException {
        LineCaptcha lineCaptcha = new LineCaptcha(140, 50, 4, 10);

        String code = lineCaptcha.getCode();

        redisTemplate.opsForValue().set(session.getId(), code,60, TimeUnit.SECONDS);

        ServletOutputStream outputStream = response.getOutputStream();

        lineCaptcha.write(outputStream);

        outputStream.close();
    }
}
